/**

 @Name：layuiAdmin 公共业务
 @Author：贤心
 @Site：http://www.layui.com/admin/
 @License：LPPL

 */

layui.define(['element','layer'],function(exports){
    var $ = layui.$
        , layer = layui.layer
        , element = layui.element;
    var obj = {

        /**
         * 格式化时间
         * @param fmt
         * @param date
         * @returns {*}
         */
        dateFormat:function (fmt, date) {
            if ( typeof date === 'number' && !isNaN(date)){
                date = new Date(date);
            }
            let ret;
            const opt = {
                "Y+": date.getFullYear().toString(),        // 年
                "m+": (date.getMonth() + 1).toString(),     // 月
                "d+": date.getDate().toString(),            // 日
                "H+": date.getHours().toString(),           // 时
                "M+": date.getMinutes().toString(),         // 分
                "S+": date.getSeconds().toString()          // 秒
                // 有其他格式化字符需求可以继续添加，必须转化成字符串
            };
            for (let k in opt) {
                ret = new RegExp("(" + k + ")").exec(fmt);
                if (ret) {
                    fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
                }
            }
            return fmt;
        },

        /**
         * 日期范围工具类
         */
        startTime:function (time,format) {
            const nowTimeDate = new Date(time);
            let date =  nowTimeDate.setHours(0, 0, 0, 0);
            if (format!=undefined){
                return this.dateFormat(format,date)
            }else {
                return  date;
            }
        },

        endTime:function (time,format) {
            const nowTimeDate = new Date(time);
            let date =  nowTimeDate.setHours(23, 59, 59, 999);
            if (format!=undefined){
                return this.dateFormat(format,date)
            }else {
                return  date;
            }
        },

        /***
         * 当前时间
         */
        getCurrentDate:function (format) {
            let date =  new Date();
            if (format!=undefined){
                return this.dateFormat(format,date)
            }else {
                return  date;
            }
        },

        /***
         * 今天的开始时间
         */
        getBeginToday:function (format) {
            let date =  new Date(new Date(new Date().toLocaleDateString()).getTime());
            if (format!=undefined){
                return this.dateFormat(format,date)
            }else {
                return  date;
            }
        },

        /***
         * 昨天开始时间
         */
        getBeginYesterday:function (format) {
            return this.startTime(this.getBeginToday() - 24 * 60 * 60 * 1000,format);
        },


        /***
         * 昨天结束时间时间
         */
        getEndYesterday:function (format) {
            return this.endTime(this.getBeginToday() - 24 * 60 * 60 * 1000,format);
        },

        /***
         * 本周的第一天时间
         */
        getBeginWeek: function (format) {
            var currentDate = this.getCurrentDate();
            var week = currentDate.getDay();

            //一天的毫秒数
            var millisecond = 1000 * 60 * 60 * 24;
            //减去的天数
            var minusDay = week != 0 ? week - 1 : 6;
            //本周 周一
            var monday = new Date(currentDate.getTime() - (minusDay * millisecond));
            return this.startTime(monday,format);
        },

        /***
         * 本周的最后一天时间
         */
        getEndWeek: function (format) {
            var currentDate = this.getCurrentDate();
            var week = currentDate.getDay();
            //一天的毫秒数
            var millisecond = 1000 * 60 * 60 * 24;
            //减去的天数
            var minusDay = week != 0 ? week - 1 : 6;
            //本周 周日
            var monday = new Date(currentDate.getTime() - (minusDay * millisecond));
            var sunday = new Date(monday.getTime() + (6 * millisecond));
            //返回
            return this.endTime(sunday,format);
        },

        /***
         * 上周的开始
         */
        getBeginLastWeek: function (format) {
            var currentDate = this.getCurrentDate();
            var first = currentDate.getDate() - currentDate.getDay() - 6;
            var startDate = new Date(currentDate.setDate(first));
            return this.startTime(startDate,format);
        },

        /***
         * 上周的结束
         */
        getEndLastWeek: function (format) {
            var currentDate = this.getCurrentDate();
            var first = currentDate.getDate() - currentDate.getDay() - 6;
            var last = first + 6;
            var endDate = new Date(currentDate.setDate(last));
            return this.endTime(endDate,format);
        },

        /***
         * 本月的第一天时间
         */
        getBeginMonth: function (format) {
            var currentDate =this.getCurrentDate();
            var currentMonth = currentDate.getMonth();
            //获得当前年份4位年
            var currentYear = currentDate.getFullYear();
            //求出本月第一天
            var firstDay = new Date(currentYear, currentMonth, 1);

            if (format!=undefined){
                return this.dateFormat(format,firstDay)
            }else {
                return  firstDay;
            }
        },

        /***
         * 本月的最后一天时间
         */
        getEndMonth: function (format) {
            //获取当前时间
            var currentDate = this.getCurrentDate();
            var fullYear = currentDate.getFullYear();
            var month = currentDate.getMonth() + 1; // getMonth 方法返回 0-11，代表1-12月
            var endOfMonth = new Date(fullYear, month, 0);
            return this.endTime(endOfMonth,format);
        },

        /***
         * 上月的第一天时间
         */
        getBeginLastMonth: function (format) {
            //获取当前时间
            var currentDate = this.getCurrentDate();
            //获得当前月份0-11
            var currentMonth = currentDate.getMonth();
            //获得当前年份4位年
            var currentYear = currentDate.getFullYear();
            //获得上一个月的第一天
            var priorMonthFirstDay = this.getPriorMonthFirstDay(currentYear, currentMonth);
            if (format!=undefined){
                return this.dateFormat(format,priorMonthFirstDay)
            }else {
                return  priorMonthFirstDay;
            }
        },

        /***
         * 上月的最后一天时间
         */
        getEndLastMonth: function (format) {
            //获取当前时间
            var currentDate = this.getCurrentDate();
            //获得当前月份0-11
            var currentMonth = currentDate.getMonth();
            //获得当前年份4位年
            var currentYear = currentDate.getFullYear();

            //当为12月的时候年份需要加1
            //月份需要更新为0 也就是下一年的第一个月
            if (currentMonth == 11) {
                currentYear++;
                currentMonth = 0; //就为
            } else {
                //否则只是月份增加,以便求的下一月的第一天
                currentMonth++;
            }

            //一天的毫秒数
            var millisecond = 1000 * 60 * 60 * 24;
            //求出上月的最后一天
            var lastDay = new Date(this.getBeginMonth().getTime() - millisecond);

            return this.endTime(lastDay,format);
        },

        /**
         * 返回上一个月的第一天Date类型
         * @param year 年
         * @param month 月
         **/
        getPriorMonthFirstDay: function (year, month) {
            //年份为0代表,是本年的第一月,所以不能减
            if (month == 0) {
                month = 11; //月份为上年的最后月份
                year--; //年份减1
                return new Date(year, month, 1);
            }
            //否则,只减去月份
            month--;
            return new Date(year, month, 1);
        },
    };

    //对外暴露的接口
    exports('xtime', obj);
});
